package com.irdstudio.tdp.executor.core.plugin.migrate.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 迁移校验指标校验结果类-[表名: 迁移校验指标校验结果(migrate_index_result)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-04
 */
public class MigrateIndexResultDao {

	/* 连接对象 */
	Connection conn = null;
	MigrationtabledataUtilDao utilDao= null;

	public MigrateIndexResultDao(Connection conn){
		this.conn = conn;
		utilDao = new MigrationtabledataUtilDao(conn);
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateIndexResult> queryMigrateIndexResultWithAll()
			throws SQLException {
		List<MigrateIndexResult> dcList = new ArrayList<MigrateIndexResult>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		MigrateIndexResult dc = null; 
		try {
			ps = conn
					.prepareStatement("SELECT * FROM migrate_index_result");
			rs = ps.executeQuery();
			while (rs.next()) {
				dc = new MigrateIndexResult();
				dc.setCheckResultId(rs.getString("check_result_id"));
				dc.setCheckIndexId(rs.getString("check_index_id"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setCheckIndexName(rs.getString("check_index_name"));
				dc.setCheckOrgCode(rs.getString("check_org_code"));
				dc.setCheckOrgName(rs.getString("check_org_name"));
				dc.setSourceTableId(rs.getString("source_table_id"));
				dc.setSourceTableCode(rs.getString("source_table_code"));
				dc.setSourceTableName(rs.getString("source_table_name"));
				dc.setSourceIndexValue(rs.getBigDecimal("source_index_value"));
				dc.setTargetTableId(rs.getString("target_table_id"));
				dc.setTargetTableCode(rs.getString("target_table_code"));
				dc.setTargetTableName(rs.getString("target_table_name"));
				dc.setTargetIndexValue(rs.getBigDecimal("target_index_value"));
				dc.setCheckExpectFlag(rs.getString("check_expect_flag"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateIndexResultWithAll is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateIndexResult> queryMigrateIndexResultWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<MigrateIndexResult> dcList = new ArrayList<MigrateIndexResult>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM migrate_index_result "
					+ szCondition + " " + szOrderBy);
			rs = ps.executeQuery();
			while (rs.next()) {
				MigrateIndexResult dc = new MigrateIndexResult();
				dc.setCheckResultId(rs.getString("check_result_id"));
				dc.setCheckIndexId(rs.getString("check_index_id"));
				dc.setSubsCode(rs.getString("subs_code"));
				dc.setCheckIndexName(rs.getString("check_index_name"));
				dc.setCheckOrgCode(rs.getString("check_org_code"));
				dc.setCheckOrgName(rs.getString("check_org_name"));
				dc.setSourceTableId(rs.getString("source_table_id"));
				dc.setSourceTableCode(rs.getString("source_table_code"));
				dc.setSourceTableName(rs.getString("source_table_name"));
				dc.setSourceIndexValue(rs.getBigDecimal("source_index_value"));
				dc.setTargetTableId(rs.getString("target_table_id"));
				dc.setTargetTableCode(rs.getString("target_table_code"));
				dc.setTargetTableName(rs.getString("target_table_name"));
				dc.setTargetIndexValue(rs.getBigDecimal("target_index_value"));
				dc.setCheckExpectFlag(rs.getString("check_expect_flag"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateIndexResultWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 *
	 * @param index
	 * @param sourcenum
	 * @param targetnum
	 * @param check_expect_flag
	 * @return
	 * @throws SQLException
	 */
	public boolean addOrUpdateOrDeleteTable(MigrateCheckIndex index,int sourcenum,int targetnum,String check_expect_flag) throws SQLException {
		//插入指标校验结果sql
		String sql= "INSERT INTO migrate_index_result (check_result_id, check_index_id, subs_code, check_index_name, check_org_code," +
				"                                  check_org_name, source_table_id, source_table_code, source_table_name," +
				"                                  source_index_value, target_table_id, target_table_code, target_table_name," +
				"                                  target_index_value, check_expect_flag)" +
				"VALUES (replace(uuid(), '-', ''),'"+index.getCheckIndexId()+"','"+index.getSubsCode()+"','" +
				""+index.getCheckIndexName()+"','"+index.getSubsCode()+"','"+index.getSubsCode()+"','" +
				""+index.getSourceTableId()+"','"+index.getSourceTableCode()+"','"+index.getSourceTableName()+"','" +
				""+sourcenum+"','"+index.getTargetTableId()+"','"+index.getTargetTableCode()+"','"+index.getTargetTableName()+"','" +
				""+targetnum+"','"+check_expect_flag+"')";
		return utilDao.addorupdateordeltable(sql);
	}

	/**
	 *
	 * @param index
	 * @return
	 * @throws SQLException
	 */
	public List<Map> selectTableList(MigrateCheckIndex index) throws SQLException {
		String selectGovernSql = " select * from migrate_govern_task where  govern_src_id='" + index.getCheckIndexId()+"'";
		return utilDao.selecttablelist(selectGovernSql);
	}

	/**
	 *
	 * @param index
	 * @return
	 * @throws SQLException
	 */
	public boolean deleteSql(MigrateCheckIndex index) throws SQLException {
		String deleteSql=" delete from migrate_index_result where check_index_id='"+index.getCheckIndexId()+"'";
		return utilDao.addorupdateordeltable(deleteSql);
	}


	/**
	 *
	 * @param index
	 * @return
	 * @throws SQLException
	 */
	public boolean addGovernSql(MigrateCheckIndex index) throws SQLException {
		String addGovernSql = " insert into batch_migrate.migrate_govern_task (govern_task_id,govern_src_id, govern_task_src," +
				" govern_task_desc, impact_analysis, migrate_comment, dev_comment, test_comment, tech_comment, busi_comment," +
				" source_incharge, target_incharge, govern_task_state, task_begin_date, task_require_finish_date, task_real_finish_date,subs_code)" +
				"VALUES ((select IFNULL(b.govern_task_id,00000) from (select LPAD(max(CAST(govern_task_id as SIGNED))+1, 5, 0) " +
				"as govern_task_id  from migrate_govern_task) b),'" + index.getCheckIndexId() + "' ,'I', '" + index.getCheckIndexName() + "', '', '', '', '', '', '', '', '', '0', '', '', '','"+index.getSubsCode()+"')";
		return utilDao.addorupdateordeltable(addGovernSql);
	}


	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
